
//
//  WSPlayerView.m
//  RunningMan
//
//  Created by 王帅 on 16/5/24.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSPlayerView.h"
#import "WSMyTool.h"
#import "WSPlayerTool.h"
@interface WSPlayerView ()
// 记录btn的点击状态
//@property (nonatomic, getter=click) BOOL isClick;

@end
@implementation WSPlayerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        
    }
    return self;
}

- (void)createSubView {

    self.labelSongName = [[UILabel alloc] initWithFrame:CGRectMake(0, 10 * FITHEIGHT, WIDTH, 30 * FITHEIGHT)];
    self.labelSongName.font = [UIFont systemFontOfSize:22 * FITWIDTH];
    self.labelSongName.textColor = [UIColor blackColor];
    self.labelSongName.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.labelSongName];
    
    self.labelSonger = [[UILabel alloc] initWithFrame:CGRectMake(0, 45 * FITHEIGHT, WIDTH, 30 * FITHEIGHT)];
    self.labelSonger.textColor = [UIColor blackColor];
    self.labelSonger.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.labelSonger];
    
    self.imageViewPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(35 * FITWIDTH, 100 * FITHEIGHT, 340 * FITWIDTH, 340 * FITHEIGHT)];
    self.imageViewPhoto.layer.cornerRadius = 170 * FITWIDTH;
    // 隐藏多余部分
    self.imageViewPhoto.layer.masksToBounds = YES;
    [self addSubview:self.imageViewPhoto];
    
    self.labelTime = [[UILabel alloc] initWithFrame:CGRectMake(0, 495 * FITHEIGHT, WIDTH, 20 * FITHEIGHT)];
    self.labelTime.textColor = [UIColor grayColor];
    self.labelTime.text = @"00:00 / 00:00";
//    self.labelTime.backgroundColor  = [UIColor clearColor];
    self.labelTime.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.labelTime];
    
    self.progress = [[UIProgressView alloc] initWithFrame:CGRectMake(20 * FITWIDTH, 520 * FITHEIGHT, WIDTH * 9 / 10, 20)];
    self.progress.progressTintColor = [UIColor grayColor];
    self.progress.trackTintColor = [UIColor colorWithRed:234/255.0 green:221/255.0 blue:193/255.0 alpha:1];
    [self addSubview:self.progress];
    
    self.btnStop = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnStop.frame = CGRectMake(170 * FITWIDTH, 580 * FITHEIGHT, 60 * FITWIDTH, 60 * FITHEIGHT);
    [self.btnStop setBackgroundImage:[UIImage imageNamed:@"iconfont-icon12"] forState:UIControlStateNormal];
    [self.btnStop addTarget:self action:@selector(stopBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.btnStop];
    
    self.btnLastMusic = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnLastMusic.frame = CGRectMake(90 * FITWIDTH, 580 * FITHEIGHT, 60 * FITWIDTH, 60 * FITHEIGHT);
    [self.btnLastMusic setImage:[UIImage imageNamed:@"iconfont-zhutishangyiqu"] forState:UIControlStateNormal];
    [self addSubview:self.btnLastMusic];
    
    self.btnNextMusic = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnNextMusic.frame = CGRectMake(250 * FITWIDTH, 580 * FITHEIGHT, 60 * FITWIDTH, 60 * FITHEIGHT);
    [self.btnNextMusic setImage:[UIImage imageNamed:@"iconfont-zhutixiayiqu"] forState:UIControlStateNormal];
    [self addSubview:self.btnNextMusic];
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(320 * FITWIDTH, 445 * FITHEIGHT, 120 * FITWIDTH, 10)];
    // 旋转90度, slider竖向
    self.slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    [self.slider addTarget:self action:@selector(changeVoice:) forControlEvents:UIControlEventValueChanged];
    self.slider.backgroundColor = [UIColor colorWithRed:234/255.0 green:221/255.0 blue:193/255.0 alpha:1];
    self.slider.thumbTintColor = [UIColor colorWithRed:234/255.0 green:221/255.0 blue:193/255.0 alpha:1];
    self.slider.minimumValue = 0;
    self.slider.maximumValue = 4;
    
    self.btnChangeVoice = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnChangeVoice.frame = CGRectMake(340 * FITWIDTH, 473 * FITHEIGHT, 40 * FITWIDTH, 40 * FITHEIGHT);
    [self.btnChangeVoice setImage:[UIImage imageNamed:@"iconfont-shengyin"] forState:UIControlStateNormal];
    [self.btnChangeVoice addTarget:self action:@selector(changeVoiceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.btnChangeVoice];
    
    
}


- (void)stopBtnClick:(UIButton *)sender {
    if ([[WSPlayerTool shareMusic] isPlaying] == YES) {
        [self.imageViewPhoto.layer removeAllAnimations];
        [self.btnStop setBackgroundImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
        [[WSPlayerTool shareMusic] pauseMusic];
        

    } else {
        [self createAnimation];

        [self.btnStop setBackgroundImage:[UIImage imageNamed:@"iconfont-icon12"] forState:UIControlStateNormal];
        [[WSPlayerTool shareMusic] playMusic];
    }

}

- (void)changeVoice:(UISlider *)slider {
    [WSPlayerTool shareMusic].player.volume = self.slider.value;
}


- (void)changeVoiceBtnClick:(UIButton *)sender {

    if (!self.isClick) {
        [self addSubview:self.slider];
        self.isClick = YES;
    } else {

        [self.slider removeFromSuperview];
        self.isClick = NO;
    }
}
- (void)createAnimation {
//    CAKeyframeAnimation *ani3 = [CAKeyframeAnimation animation];
//    ani3.keyPath = @"transform.rotation";
//    NSValue *val0 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
//    NSValue *val1 = [NSValue valueWithCGPoint:CGPointMake(5, 5)];
//    NSValue *val2 = [NSValue valueWithCGPoint:CGPointMake(10, 10)];
//    NSValue *val3 = [NSValue valueWithCGPoint:CGPointMake(15, 15)];
//    NSValue *val4 = [NSValue valueWithCGPoint:CGPointMake(20, 20)];
//    NSValue *val5 = [NSValue valueWithCGPoint:CGPointMake(25, 25)];
//    ani3.values = @[val0, val1, val2, val3, val4, val5];
//    ani3.duration = 20;
//    ani3.repeatCount = MAXFLOAT;
//    [self.imageViewPhoto.layer addAnimation:ani3 forKey:nil];
    
    //创建一个绕z轴选择的动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //旋转一周
    animation.toValue = @(2*M_PI);
    //循环次数，尽量大
    animation.repeatCount = MAXFLOAT;
    //执行一次动画需要的时间
    animation.duration = 15.0f;
    [self.imageViewPhoto.layer addAnimation:animation forKey:@"rotationAnimation"];
    
    
//    self.imageViewPhoto.transform = CGAffineTransformRotate(self.imageViewPhoto.transform, M_PI/360);

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
